package com.icarsaas.saas.tenant.test.web;

import com.icarsaas.saas.common.page.IPager;
import com.icarsaas.saas.common.page.Page;
import com.icarsaas.saas.common.page.SoulPage;
import com.icarsaas.saas.common.utils.ResultVoUtil;
import com.icarsaas.saas.component.actionLog.annotation.EntityParam;
import com.icarsaas.saas.tenant.test.model.Committee;
import com.icarsaas.saas.tenant.test.service.CommitteeService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 评审委员会Controller
 * @author jianghong.huang @date 20180206
 * 2018年1月23日
 */
@Controller @RequestMapping(value = "/buz/committee")
public class CommitteeController {
	@Autowired private CommitteeService committeeService;
	
	@ModelAttribute public Committee get(@RequestParam(required = false) String id) {
		Committee entity = null;
		if (StringUtils.isNotBlank(id) && null != id) entity = committeeService.get(id);
		else entity = new Committee();
		return entity;
	}
	
	@GetMapping("/list")
    @RequiresPermissions("system:user:jqgrid")
	public String list() {
		return "/demo/index";
	}
	
	@SuppressWarnings("unchecked")
	@PostMapping(value = "listData")
	@ResponseBody
	public Object listData(IPager<Map<String, Object>> pager, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = pager.getParams();
		Page<Map<String, Object>> page = committeeService.listData(pager.pageParams(request, response), map);
		return pager.pageResult(page);
	}
	
	@GetMapping(value = "committeeForm")
	public String form(Committee entity, String view, Model model) {
		model.addAttribute("entity", entity);
		if (StringUtils.isNotEmpty(view)) model.addAttribute("view", view);
		return "/demo/committeeForm";
	}

	//@RequiresPermissions("prlife:investModel:edit")
	@PostMapping(value = "save") @ResponseBody//@Valid
	public Object save(@EntityParam Committee entity, Model model) throws IOException {
		entity = committeeService.save(entity);//保存并返回
		return ResultVoUtil.success(entity);//返回成功的标记
	}
	@RequiresPermissions("prlife:committee:edit")
	@PostMapping(value = "delete") @ResponseBody
	public Object delete(String[] ids) {
		int i = 0;
		if (ids != null && ids.length > 0) {
			for (String id : ids) {
				committeeService.delete(id);
				i++;
			}
		}
		return i;
	}
	
	@GetMapping("/soultable")
    @RequiresPermissions("system:user:jqgrid")
	public String soultable() {
		return "/demo/soultable";
	}
	
	@PostMapping("listSoulTable") @ResponseBody
	public Object listSoulTable(SoulPage<Map<String, Object>> page, @RequestBody Map<String, Object> params) {
//	public Object listSoulTable(SoulPage<Map<String, Object>> soulPage, @RequestBody Map<String, Object> params) {
//	public Object listSoulTable(SoulPage<List<Committee>> soulPage, @RequestBody Map<String, Object> params) {
		page.setParams(params, page);
		page = committeeService.listSoulData(page, params);
		return page;
	}
	
	@GetMapping("/iconselect")
	public String iconselect() {
		return "/demo/iconselect";
	}
	
}
